package cn.vivi.recyclercomp.test;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import cn.vivi.recyclercomp.R;
import cn.vivi.recyclercomp.custom.CustomGridViewFragment;
import cn.vivi.recyclercomp.custom.CustomListViewFragment;
import cn.vivi.recyclercomp.custom.CustomListViewHeaderFragment;

@SuppressLint("ValidFragment")
public class BottomMenuMainFragment extends Fragment implements CompoundButton.OnCheckedChangeListener,
		ViewPager.OnPageChangeListener {

	private static final String[] CONTENT = new String[] { "test1", "test2", "test3", "test4" };


	public BottomMenuMainFragment() {
		super();
	}

	ViewPager mPager = null;
	RadioGroup mRgBottomMenu = null;
	private RadioButton mRbtn1;
	private RadioButton mRbtn2;
	private RadioButton mRbtn3;
	private RadioButton mRbtn4;

	View rootView = null;


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_bottom_menu_main, container, false);

		FragmentPagerAdapter adapter = new GoogleMusicAdapter(getActivity().getSupportFragmentManager());
		mPager = (ViewPager) rootView.findViewById(R.id.vp_home);
		mPager.setAdapter(adapter);
		mPager.setOnPageChangeListener(this);
		mPager.setOffscreenPageLimit(4);

		mRgBottomMenu = (RadioGroup) rootView.findViewById(R.id.rg_bottom);

		mRbtn1 = (RadioButton) rootView.findViewById(R.id.rbtn1);
		mRbtn2 = (RadioButton) rootView.findViewById(R.id.rbtn2);
		mRbtn3 = (RadioButton) rootView.findViewById(R.id.rbtn3);
		mRbtn4 = (RadioButton) rootView.findViewById(R.id.rbtn4);

		mRbtn1.setOnCheckedChangeListener(this);
		mRbtn2.setOnCheckedChangeListener(this);
		mRbtn3.setOnCheckedChangeListener(this);
		mRbtn4.setOnCheckedChangeListener(this);

		return rootView;
	}


	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	class GoogleMusicAdapter extends FragmentPagerAdapter {

		public GoogleMusicAdapter(FragmentManager fm) {
			super(fm);
		}


		@Override
		public Fragment getItem(int position) {
			Fragment frg = null;
			switch (position) {
			case 0:
				frg = new CustomGridViewFragment(2);
				break;

			case 1:
				frg = new CustomGridViewFragment(5);
				break;

			case 2:
				frg = new CustomListViewFragment();
				break;

			case 3:
				frg = new CustomListViewHeaderFragment();
				break;

			default:
				frg = new CustomListViewFragment();
			}
			return frg;
		}


		@Override
		public int getCount() {
			return CONTENT.length;
		}
	}


	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}


	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}


	@Override
	public void onPageSelected(int posi) {
		RadioButton rbtn = null;
		switch (posi) {
		case 0:
			rbtn = mRbtn1;
			break;

		case 1:
			rbtn = mRbtn2;
			break;
		case 2:
			rbtn = mRbtn3;
			break;
		case 3:
			rbtn = mRbtn4;
			break;
		}
		if (rbtn != null && !rbtn.isChecked()) {
			mRgBottomMenu.check(rbtn.getId());
		}
	}


	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			int viewId = buttonView.getId();
			if (viewId == R.id.rbtn1) {
				if (mPager.getCurrentItem() != 0) {
					mPager.setCurrentItem(0, false);
				}
			} else if (viewId == R.id.rbtn2) {
				if (mPager.getCurrentItem() != 1) {
					mPager.setCurrentItem(1, false);
				}
			} else if (viewId == R.id.rbtn3) {
				if (mPager.getCurrentItem() != 2) {
					mPager.setCurrentItem(2, false);
				}
			} else if (viewId == R.id.rbtn4) {
				if (mPager.getCurrentItem() != 3) {
					mPager.setCurrentItem(3, false);
				}
			}
		}

	}

}
